package cn.shadow.concurrent;

import org.springframework.lang.NonNull;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

import static java.lang.Thread.MAX_PRIORITY;

public class DefaultThreadFactory implements ThreadFactory {
    private String threadName;
    private AtomicInteger count;

    public DefaultThreadFactory(String threadName) {
        this.threadName = threadName;
        this.count = new AtomicInteger(0);
    }

    @Override
    public Thread newThread(@NonNull Runnable task) {
        Thread thread = new Thread(task, threadName + "_" + count.getAndIncrement());
        thread.setPriority(MAX_PRIORITY);
        return thread;
    }
}
